﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class CS0011: BaseRule
    {
        int ALLOWED_TICKS_BETWEEN_HOME_AND_AWAY = 2;
        double CS_0_0_MIN_ODDS = 5;
        double CS_1_1_MIN_ODDS = 2000;
        int CS_ALLOWED_TICKS_BETWEEN_0_1_AND_1_0 = 2;
        double CS_MIN_MATCHED = 500;


        public CS0011(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices) : base()
         {

             MarketsUsedInside = new string[] { "match odds", "correct score"};

             try
             {

                 ALLOWED_TICKS_BETWEEN_HOME_AND_AWAY = Convert.ToInt32(config["Rule.CS0011.ALLOWED_TICKS_BETWEEN_HOME_AND_AWAY"]);
                 CS_0_0_MIN_ODDS = Convert.ToDouble(config["Rule.CS0011.CS_0_0_MIN_ODDS"]);
                 CS_1_1_MIN_ODDS = Convert.ToDouble(config["Rule.CS0011.CS_1_1_MIN_ODDS"]);
                 CS_ALLOWED_TICKS_BETWEEN_0_1_AND_1_0 = Convert.ToInt32(config["Rule.CS0011.CS_ALLOWED_TICKS_BETWEEN_0_1_AND_1_0"]);
                 CS_MIN_MATCHED = Convert.ToDouble(config["Rule.CS0011.CS_MIN_MATCHED"]);

             }
             catch (Exception e)
             {
                 Logger.LogError(e.ToString(), true, true);
             }

             string criteria = @"" + ALLOWED_TICKS_BETWEEN_HOME_AND_AWAY + "/" +
                 CS_0_0_MIN_ODDS + "/" +
                 CS_1_1_MIN_ODDS + "/" +
                 CS_ALLOWED_TICKS_BETWEEN_0_1_AND_1_0 + "/" +
                 CS_MIN_MATCHED;

             Logger.Log("============================================================================", true, true);
             Logger.Log("Matches qualified for Correct score 0-0, 1-1 (" + criteria + ")", true, true);
             Logger.Log("----------------------------------------------------------------------------", true, true);

             if (!CheckRequiredMarkets()) { return; } 

             string[] games = FindSoccerGames.GetMatches();

             int selectionCount = 0;
             foreach (var game in games)
             {
                 try
                 {
                             //MyMarket market_ou = FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals");
                             //MarketPricesCompressed mpc_ou = marketprices[market_ou.marketId];

                             MyMarket market_cs = FindSoccerGames.GetMarket(game, "correct score");
                             MarketPricesCompressed mpc_cs = marketprices[market_cs.marketId];

                             MyMarket market_mo = FindSoccerGames.GetMarket(game, "match odds");
                             MarketPricesCompressed mpc_mo = marketprices[market_mo.marketId];



                         
                         double homeodds = mpc_mo.GetBestBackOdds(0);
                         double awayodds = mpc_mo.GetBestBackOdds(1);
                         bool MATCHODDS_DIFF_OK = BetfairTicks.IsWithinAllowedTickRange((decimal)homeodds, (decimal)awayodds, ALLOWED_TICKS_BETWEEN_HOME_AND_AWAY);

                         if (MATCHODDS_DIFF_OK)
                         { 
                             //double o25odds = mpc_ou.GetBestBackOdds(1);
                             double matched_cs = mpc_cs.TotalMatched;
                             double odds_00 = mpc_cs.GetBestBackOdds(0);
                             double odds_11 = mpc_cs.GetBestBackOdds(5);
                             double odds_01 = mpc_cs.GetBestBackOdds(1);
                             double odds_10 = mpc_cs.GetBestBackOdds(4);
                             bool CS_01_01_DIFF_OK = BetfairTicks.IsWithinAllowedTickRange((decimal)odds_01, (decimal)odds_10, CS_ALLOWED_TICKS_BETWEEN_0_1_AND_1_0);

                             if (matched_cs >= CS_MIN_MATCHED &&
                                 odds_00 >= CS_0_0_MIN_ODDS &&
                                 odds_11 >= CS_1_1_MIN_ODDS &&
                                 CS_01_01_DIFF_OK
                                 )
                             {

                                 Logger.Log(market_cs.marketTime + ", " + BF.convertPathToGame(game) + ", home: " + homeodds + ", away: " + awayodds + ", 0-0: " + odds_00 + ", 1-1: " + odds_11 +  ", 1-0: " + odds_10 + ", 0-1: " + odds_01 +", " + config["CURRENCY_SIGN"] + matched_cs, true, true);
                                 selectionCount++;

                             }
                         }

                 }
                 catch (Exception ex)
                 {

                     Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);

                 }

             }
             if (selectionCount == 0)
             {
                 Logger.Log("No matches", true, true);
             }
         }
    }
}


